<script  setup>
import { ref, reactive,toRaw } from "vue";
import {login} from '@/api/index.js'
import { useRouter } from "vue-router";
import store from "@/store/index";
import { ElMessage } from 'element-plus'
const router= useRouter()
const userobj = {
  userName: 'admin',
  userPwd: 'admin',
};
const rulesobj={
  userName:[
      {required:true,message:'请输入用户名',trigger:'blur'}
  ],
   userPwd:[
      {required:true,message:'请输入密码',trigger:'blur'}
  ],
}
const user = reactive(userobj);
const rules=reactive(rulesobj)
const ruleFormRef = ref()
const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate(async(valid) => {
    if (valid) {
     await login(toRaw(userobj)).then(res=>{
         if(res?.code==200){
            ElMessage({
              message: '欢迎回来',
               type: 'success',
            })
           store.commit('saveUserInfo', res.data)
           setTimeout(() => {
             router.push('/')
           }, 1000);
         }
     })
    } else {
      console.log('error submit!')
      return false
    }
  })
}

</script>

<template>
  <div class="login-wrapper">
    <div class="modal">
      <el-form :model="user" status-icon :rules="rules" ref="ruleFormRef">
        <div class="title">Mangoxin</div>
        <el-form-item prop="userName">
          <el-input
            v-model="user.userName"
            placeholder="Please input useruserName"
            clearable
          >
            <template #prepend>
              <el-icon><user-filled /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="userPwd">
          <el-input
            v-model="user.userPwd"
            placeholder="Please input userPwd"
            clearable
          >
            <template #prepend>
              <el-icon><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" @click="submitForm(ruleFormRef)">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss">
.login-wrapper {
  background: url("@/assets/login-bg.jpeg")  #f9fcff;
  background-size: 100%;
  // background-position-y: -50px;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 100vh;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0px 0px 10px 3px #c7c9cb4d;
    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .login-btn {
      width: 100%;
    }
    .el-input__prefix {
      top: 9px;
    }
  }
}
</style>
